package com.jingli.pile.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jingli.pile.configuration.PushProperties;
import com.jingli.pile.domain.OperatorInfoData;
import com.jingli.pile.domain.CommonDataResult;
import com.jingli.pile.entity.PlatformInfo;
import com.jingli.pile.entity.SupOperatorInfo;
import com.jingli.pile.request.TokenQueryRequest;
import com.jingli.pile.service.PlatformInfoService;
import com.jingli.pile.service.QueryOperatorService;
import com.jingli.pile.util.HmacMd5Signature;
import com.jingli.pile.util.MessageEncryption;
import com.jingli.pile.util.PileSignUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping
public class OperationApiController {
    private Logger logger = LoggerFactory.getLogger(OperationApiController.class);
    @Autowired
    private QueryOperatorService queryOperatorService;
    @Autowired
    private PushProperties pushProperties;
    @RequestMapping("/evcs/v1/supervise_query_operator_info")
    @ResponseBody
    public CommonDataResult queryOperatorInfo(@RequestBody TokenQueryRequest queryRequest) throws Exception {
        logger.info("查询运营商信息参数:{}",JSONObject.toJSONString(queryRequest));
        String decrypted = MessageEncryption.decrypt(queryRequest.getData(), pushProperties.getDataSecret(), pushProperties.getDataSecretIV());
        logger.info("解密后的运营商查询信息为:{}",decrypted);
        Integer pageNo,pageSize;
        if (!StringUtils.isEmpty(decrypted)){
            JSONObject jsonObject = JSONObject.parseObject(decrypted);
            pageNo = jsonObject.getInteger("PageNo");
            pageSize = jsonObject.getInteger("PageSize");
        }else {
            return CommonDataResult.failure(1002,"加解密异常","","");
        }
        IPage<SupOperatorInfo> supOperatorInfoIPage = queryOperatorService.queryOperatorInfos(pageNo, pageSize);
        OperatorInfoData infoData = new OperatorInfoData();
        infoData.setOperatorInfos(supOperatorInfoIPage.getRecords());
        infoData.setItemSize((int) supOperatorInfoIPage.getTotal());
        infoData.setPageNo((int) supOperatorInfoIPage.getCurrent());
        infoData.setPageCount((int) supOperatorInfoIPage.getPages());
        String signParams = PileSignUtils.signParams(JSONObject.toJSONString(infoData), pushProperties.getDataSecret(), pushProperties.getDataSecretIV());
        String sign = HmacMd5Signature.hmacMd5(signParams,pushProperties.getSignSecret());
        return CommonDataResult.success(signParams,sign);
    }
}
